home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Problem Negating an Unsigned Char
- Date: 4 Mar 1996 11:18:08 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hfflgINNd9v@keats.ugrad.cs.ubc.ca>
- References: <Dnnros.Lq.0.-s@hkusuc.hku.hk> <4he27sINNdel@keats.ugrad.cs.ubc.ca> <4he5f0$acv@solutions.solon.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4he5f0$acv@solutions.solon.com>,
- Peter Seebach <seebs@solutions.solon.com> wrote:
- >In article <4he27sINNdel@keats.ugrad.cs.ubc.ca>,
- >Kazimir Kylheku <c2a192@ugrad.cs.ubc.ca> wrote:
- >>To make it portable, you must manually mask for the lower eight bits:
- >
- >> if (a == (~b & 0xff))
- >
- >Close, but the cigar yet escapes you.
-
- I don't think so.
-
- > if (a == (~b & ((1 << CHAR_BIT) - 1)))
-
- I assumed that the poster is interested in manipulating eight bit bytes, which
- he defines using two digit hexadecimal constants. I know that he wants only
- eight bits, because his program suggests that he expects 0x11 to be the
- complement of 0xEE.
-
- The char datatype is guaranteed to hold eight bits, thus my masking is valid
- and portable. He did not say that he wants to utilize the full,
- machine-specific precision of the unsigned char datatype, and is in fact
- probably oblivious to the fact that chars can hold more than eight bits.
-
- >Further, you can't do this portably; if sizeof(long) is 1, the
- >shift is illegal.
-
- It's your shift, not mine. If you want to use the full precision, all you have
- to do is invert and cast through unsigned char.
-
- if (a == (unsigned char) ~b)
-
- b gets promoted to unsigned int, and then is inverted. The cast to unsigned
- char truncates it down to CHAR_BITS precision. It's then promoted again to
- unsigned int. Effectively, you have inverted just the bits that belonged to the
- original unsigned char value of b.
-
- The poster did say that this works for him, but he is not aware that it is not
- the portable eight bit arithmetic that he wants. It just so happens that on his
- implementation, a char is eight bits, so the complement of 0xEE is the 0x11
- that he expects, rather than 0x111 (9 bit char), 0x311 (10 bit char) and so
- forth.
- --
-
-